package com.sinoglobal.dumping.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.sinoglobal.dumping.R;
import com.sinoglobal.dumping.api.Dumpling_RemoteImpl;
import com.sinoglobal.dumping.base.Dumpling_ShareAbstractActivity;
import com.sinoglobal.dumping.base.Dumpling_SinoAsyncTask;
import com.sinoglobal.dumping.base.Dumpling_SinoConfig;
import com.sinoglobal.dumping.base.Dumpling_SinoConstans;
import com.sinoglobal.dumping.bean.Dumpling_ShowCardInfoVo;
import com.squareup.picasso.Picasso;

public class Dumpling_MyCardDetailActivity extends Dumpling_ShareAbstractActivity {

    private ImageView myCardPhoto;
    private TextView myCardCongratulation;
    private Button myCardShare;
    private int imageWidth;
    private int imageHeight;
    private String shareUrl;
    private String shareContent;
    private String shareTitle;
    private String sharePicUrl;
    private boolean goMainActivity = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dumpling_activity_my_card_detail);
        initView();
    }

    private void initView() {
        int fromType = getIntent().getIntExtra("fromType", 1);
        mTemplateTitleText.setText("贺卡");
        View view = getView(R.id.title_right_btn_share);
        hideView(mTemplateRightText, mTemplateRightImg, view);
        myCardPhoto = (ImageView) findViewById(R.id.dumpling_my_card_photo);
        myCardCongratulation = (TextView) findViewById(R.id.dumpling_my_card_congratulation);
        myCardShare = (Button) findViewById(R.id.dumpling_my_card_send);
        myCardPhoto.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                imageWidth = myCardPhoto.getWidth();
                imageHeight = myCardPhoto.getHeight();
            }
        });
        if (1 == fromType) {
            myCardShare.setVisibility(View.INVISIBLE);
        } else if (2 == fromType) {
            initShareDate();
            myCardShare.setOnClickListener(this);
        }
        initData();
    }

    private void initData() {
        new Dumpling_SinoAsyncTask<Void, Void, Dumpling_ShowCardInfoVo>(Dumpling_MyCardDetailActivity.this) {
            @Override
            public Dumpling_ShowCardInfoVo before(Void... params) throws Exception {
                return Dumpling_RemoteImpl.getInstance().showCard(getIntent().getStringExtra("DumplingUserPutcardId"));
            }

            @Override
            public void after(Dumpling_ShowCardInfoVo dumpling_showCardInfoVo) {
                Picasso.with(Dumpling_MyCardDetailActivity.this)
                        .load(dumpling_showCardInfoVo.getCardPic())
                        .placeholder(R.drawable.dumpling_card_photo_default)
                        .error(R.drawable.dumpling_card_photo_default)
                        .resize(imageWidth, imageHeight)
                        .centerCrop()
                        .into(myCardPhoto);
                myCardCongratulation.setText(dumpling_showCardInfoVo.getCardWish());
            }

            @Override
            public void exception() {

            }
        }.execute();
    }

    private void initShareDate() {
        String userPutCardId = getIntent().getStringExtra("DumplingUserPutcardId");
        if (Dumpling_SinoConfig.sCardShare == null) {
            showToast("访问网络失败，请稍后再试");
            goIntent(Dumpling_MainActivity.class);
        } else {
            shareUrl = Dumpling_SinoConfig.sCardShare.getResultList().getUrl() + "?name=" + Dumpling_SinoConstans.USER_NAME + "&productCode=" + Dumpling_SinoConstans.PRODUCT_CODE + "&pannikin_id=" + userPutCardId;
            shareContent = Dumpling_SinoConfig.sCardShare.getResultList().getContent();
            shareTitle = Dumpling_SinoConfig.sCardShare.getResultList().getTitle();
            sharePicUrl = Dumpling_SinoConfig.sCardShare.getResultList().getPicurl();
            if (!shareUrl.contains("http://")) {
                shareUrl = "http://" + shareUrl;
            }
        }
    }

    @Override
    public void onAntiViolence(View view) {
        if (R.id.dumpling_my_card_send == view.getId()) {
            setShare(shareContent, shareTitle, null, shareUrl, 0, sharePicUrl);
            goMainActivity = true;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (goMainActivity) {
            goIntent(Dumpling_MainActivity.class);
        }
    }

    /**
     * 分享成功
     */
    protected void shareSuccess() {
        goIntent(Dumpling_MainActivity.class);
    }
}
